﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;

namespace Smart
{
    static class Program
    {
        public static Sessions Session = null;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [MTAThread]
        static void Main()
        {
            AppDomain currentDomain = AppDomain.CurrentDomain;
            currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);

            Session = Sessions.CreateSession;

            if (!File.Exists(Methods.StartupPath() + "\\ayarlar.ini"))
            {
                using (frmSettings set = new frmSettings())
                {
                    set.ShowDialog();
                }
            }

            Entity.Get.Init();
            string cnnStr = "Data Source=" + Path.Combine(Methods.StartupPath(), "DB.sdf");
            SqlCeClient.Get.CnnStr = cnnStr;
            DbSqlServer.Get.CnnString = string.Format("data source={0};database={1};user id={2};password={3}", Program.Session[Settings.Sunucu], Program.Session[Settings.Veritabani], Program.Session[Settings.VtKullaniciAdi], Program.Session[Settings.VtSifre]);

            DialogResult res = MessageBox.Show("Veritabanı kontrol edilsin mi ?", "Dikkat", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
            if (res == DialogResult.Yes)
            {
                Entity.Get.CreateOrUpdateDb();
            }

            frmKullaniciGiris gir = new frmKullaniciGiris();
            if (gir.ShowDialog() == DialogResult.OK)
                Application.Run(new frmMain());
        }

        static void MyHandler(object sender, UnhandledExceptionEventArgs args)
        {
            Exception e = (Exception)args.ExceptionObject;
            ExceptionHandling.Hand(e);
        }
    }
}